package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.QbAdapter;
import com.xfly.app.bean.QbBean;
import com.xfly.app.bean.UserBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WalletActivity extends BaseMvpActivity implements View.OnClickListener {


    TitleBarView title_bar;

    TextView num,rcbtn;
    TextView all,sr,zc,tx;

    View view1,view2,view3;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    PullToRefreshLayout refresh_view;
    PullableListView  listview;

    String type = "";

    int page = 1;

    QbAdapter mQbAdapter;

    List<QbBean> list = new ArrayList<>();

    LinearLayout yhk;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_qb);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        num = findViewById(R.id.num);
        all = findViewById(R.id.all);
        sr = findViewById(R.id.sr);
        zc = findViewById(R.id.zc);
        rcbtn = findViewById(R.id.rcbtn);
        yhk = findViewById(R.id.yhk);
        tx = findViewById(R.id.tx);

        view1 = findViewById(R.id.view1);
        view2 = findViewById(R.id.view2);
        view3 = findViewById(R.id.view3);
        refresh_view = findViewById(R.id.refresh_view);
        listview = findViewById(R.id.listview);

        title_bar.setText("我的钱包");
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        all.setOnClickListener(this);
        sr.setOnClickListener(this);
        zc.setOnClickListener(this);

        refresh_view.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getlist();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getlist();
            }
        });

        yhk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),BankListActivity.class));
            }
        });

        mQbAdapter = new QbAdapter(getContext(),list);
        listview.setAdapter(mQbAdapter);

        rcbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),RechargeActivity.class);
                startActivity(intent);
            }
        });

        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),TxActivity.class));
            }
        });

        getlist();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData(){
        apiDataRepository.myInfo(null, new ApiNetResponse<UserBean>() {
            @Override
            public void onSuccess(UserBean data) {
                num.setText(data.getMoney());
            }
        });
    }

    private void getlist(){
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("mold",type);
        apiDataRepository.qblist(map, new ApiNetResponse<List<QbBean>>() {
            @Override
            public void onSuccess(List<QbBean> data) {
                if(refresh_view!=null){
                    refresh_view.refreshFinish(PullToRefreshLayout.SUCCEED);
                    refresh_view.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if(page == 1){
                    list.clear();
                }
                list.addAll(data);
                mQbAdapter.pushData(list);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.all:
                all.setTextColor(0xff242424);
                sr.setTextColor(0xff666666);
                zc.setTextColor(0xff666666);
                view1.setVisibility(View.VISIBLE);
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.INVISIBLE);
                type = "";
                break;
            case R.id.sr:
                all.setTextColor(0xff666666);
                sr.setTextColor(0xff242424);
                zc.setTextColor(0xff666666);
                view1.setVisibility(View.INVISIBLE);
                view2.setVisibility(View.VISIBLE);
                view3.setVisibility(View.INVISIBLE);
                type = "in";
                break;
            case R.id.zc:
                all.setTextColor(0xff666666);
                sr.setTextColor(0xff666666);
                zc.setTextColor(0xff242424);
                view1.setVisibility(View.INVISIBLE);
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.VISIBLE);
                type = "out";
                break;
        }
        page = 1;
        getlist();
    }
}
